package com.AAA.controller.sds;


import com.AAA.Util.EasyExcelUtil;
import com.AAA.entity.*;
import com.AAA.entity.mht.TMht;
import com.AAA.entity.sds.TSds;
import com.AAA.service.Impl.sds.TSdsRecordDedtailServiceImpl;
import com.AAA.service.sds.ITsdsService;
import com.AAA.service.Impl.sds.TsdsServiceimpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("tsds")
public class TSdsController {
    @Autowired
    private ITsdsService service;
    @Autowired
    private TSdsRecordDedtailServiceImpl dedtailService;
    //批量添加
    @Autowired
    private TsdsServiceimpl serviceimpl;
    @PostMapping("addall")
    @ResponseBody
    public LayData addAll(HttpServletRequest request, Model model, MultipartFile file) throws IOException {
        List<Object> objects = EasyExcelUtil.readExcel(file, new TSds(), 1, 1);
        int count=0;
        List<Integer> list=new ArrayList<>();
        LayData layData=new LayData();
        for (Object t:objects){
            TSds tSds= (TSds) t;
            count++;
            Integer addall = serviceimpl.addall(tSds);
            if (addall!=addall){
                list.add(count);
            }
        }
        if (list.size()==0){
            layData.setMsg("添加成功");
        }else {
            layData.setData("有第"+list.toString()+"条未添加成功");
        }
        return layData;
    }

    //添加 and 修改
    @PostMapping("add")
    @ResponseBody
    public LayData add(TSds tSds){
        if (tSds.getQid()==null){
            return new LayData(serviceimpl.add(tSds));
        }else {
            return new LayData(serviceimpl.edit(tSds));
        }
    }

    //分页查询
    @RequestMapping("queryall")
    @ResponseBody
    public LayData queryall(MyPage myPage, TSds tSds){
        PageInfo pageInfo=service.query(myPage,tSds);
        return new LayData(pageInfo);
    }
    //删除
    @PostMapping("del")
    @ResponseBody
    public LayData delete(Integer qid){
        TSds tSds  = service.selectById(qid);
        Integer tid = tSds.getTid();
        boolean b = dedtailService.selectByTid(tid);
        if(b){
            return new LayData(0);
        }else {
            boolean delete = service.delete(qid);
            return new LayData(delete );
        }

    }


    /**
     * fpa做题页面
     * @param myPage
     * @param request
     * @return
     */
    @GetMapping("index")
    public String getIndexAllSub(MyPage myPage, HttpServletRequest request){
        myPage.setLimit(30);
        myPage.setPage(1);
        PageInfo all = service.getAll(myPage);
        request.setAttribute("allsds",all);

        return "sds/index";

    }



    // 批量导入数据
    @Resource
    private TsdsServiceimpl service1;
    @PostMapping(value = "/import")
    @ResponseBody
    public LayData storageService(MultipartFile file) {

        service1.dealFile(file);
        return new LayData(true);
    }


    @GetMapping
    @ResponseBody
    public LayData getAllSub(MyPage myPage){

        return new LayData(service1.getAll(myPage));
    }

}
